package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.User;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "用户管理")
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    /**
     * 分页查询用户列表
     * @param pageNum
     * @param pageSize
     * @param userDto
     * @return
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageResponse<UserVo>> getUserList(@PathVariable Integer pageNum,
                                                            @PathVariable Integer pageSize,
                                                            @RequestBody UserDto userDto){
        log.info("分页查询用户列表接口，入参pageNum={},pageSize={},{}",pageNum,pageSize,userDto);
        PageResponse<UserVo> response = userService.getUserList(pageNum,pageSize,userDto);
        return ResponseResult.success(response);
    }

    /**
     * 新增用户
     * @param userDto
     * @return
     */
    @PutMapping
    public ResponseResult addUser(@RequestBody UserDto userDto){
        log.info("新增用户接口，入参={}",userDto);
        userService.addUser(userDto);
        return ResponseResult.success();
    }

    /**
     * 更新用户信息
     * @param userDto
     * @return
     */
    @PatchMapping
    public ResponseResult updateUser(@RequestBody UserDto userDto){
        log.info("更新用户信息接口，入参={}",userDto);
        userService.updateUser(userDto);
        return ResponseResult.success();
    }

    /**
     * 查询当前用户
     * @return
     */
    @GetMapping("/current-user")
    public ResponseResult<UserVo> getCurrentUser(){
        log.info("查询当前用户接口");
        UserVo userVo = userService.getCurrentUser();
        return ResponseResult.success(userVo);
    }

    /**
     * 修改用户状态信息
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult updateStatus(@PathVariable Long id,
                                       @PathVariable String status){
        log.info("修改用户状态信息接口，入参id={}，status={}",id,status);
        userService.updateUserStatus(id,status);
        return ResponseResult.success();
    }

    /**
     * 删除用户
     * @param userIds
     * @return
     */
    @DeleteMapping("/remove/{userIds}")
    public ResponseResult deleteById(@PathVariable Long userIds){
        log.info("删除用户接口，入参id={}",userIds);
        userService.deleteById(userIds);
        return ResponseResult.success();
    }

    /**
     * 账号密码重置
     * @param userId
     * @return
     */
    @PostMapping("/reset-passwords/{userId}")
    public ResponseResult updatePassWord(@PathVariable Long userId){
        log.info("账号密码重置接口，入参id={}",userId);
        userService.updatePassWord(userId);
        return ResponseResult.success();
    }
}
